package com.edu.whty.task.sync;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.edu.whty.common.tools.HttpClient;
import com.edu.whty.common.tools.OpenUtil;
import com.edu.whty.entity.cloud.TbCourseRes;
import com.edu.whty.entity.cloud.TbCourseUser;
import com.edu.whty.service.CourseService;
import com.edu.whty.service.UserTextBookService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SyncDataTask {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Value("${rrtbk.url}")
    private String rrtbkUrl;

    @Value("${rrtbk.api.listUserTextBook}")
    private String listUserTextBook;

    @Value("${rrtbk.api.queryChapters}")
    private String queryChapters;

    @Value("${rrtbk.api.initCourseResList}")
    private String initCourseResList;

    @Value("${rrtbk.api.initCourseUserList}")
    private String initCourseUserList;

    @Value("${rrtbk.orgId}")
    private String orgId;

    @Value("${rrtbk.isCloud}")
    private boolean isCloud;


    @Autowired
    private CourseService courseService;

    @Async
    @Scheduled(cron = "0 0/5 * * * ?")  //每30分钟执行一次
    public void upSyncData() {
        if (!isCloud) {
            logger.info("starting sync data task");

            try {

                //同步资源
                String courseUserUrl = rrtbkUrl + initCourseUserList+"?accessToken=" + OpenUtil.createTokenByUrl(rrtbkUrl);
                JSONObject courseUserParam = new JSONObject();
                courseUserParam.put("orgId", orgId);
                logger.info("初始化courseUser url:" + courseUserUrl + ";请求参数:" + courseUserParam.toJSONString());
                String courseUserResponse = HttpClient.doJSONPostMethod(courseUserUrl, courseUserParam.toJSONString());
                JSONObject courseUserResponseObject = JSONObject.parseObject(courseUserResponse);
                List<TbCourseUser> courseUserList = new ArrayList<TbCourseUser>();
                if (courseUserResponseObject.getString("resCode").equals("000000")) {
                    JSONArray array = courseUserResponseObject.getJSONArray("data");
                    for (Object o : array) {
                        String jsonString = JSONObject.toJSONString(o);
                        TbCourseUser courseUser = JSONObject.parseObject(jsonString, TbCourseUser.class);
                        courseUserList.add(courseUser);
                    }
                } else {
                    logger.error("sync tb_course_user failed:" + courseUserResponseObject.getString("resMsg"));
                }

                String courseResUrl = rrtbkUrl + initCourseResList+"?accessToken=" + OpenUtil.createTokenByUrl(rrtbkUrl);
                JSONObject courseResParam = new JSONObject();
                courseResParam.put("orgId", orgId);
                logger.info("初始化courseRes url:" + courseResUrl + ";请求参数:" + courseResParam.toJSONString());
                String courseResResponse = HttpClient.doJSONPostMethod(courseResUrl, courseResParam.toJSONString());
                JSONObject courseResResponseObject = JSONObject.parseObject(courseResResponse);
                List<TbCourseRes> courseResList = new ArrayList<TbCourseRes>();
                if (courseResResponseObject.getString("resCode").equals("000000")) {
                    JSONArray array = courseResResponseObject.getJSONArray("data");
                    for (Object o : array) {
                        String jsonString = JSONObject.toJSONString(o);
                        TbCourseRes courseRes = JSONObject.parseObject(jsonString, TbCourseRes.class);
                        courseResList.add(courseRes);
                    }
                } else {
                    logger.error("sync tb_course_res failed:" + courseResResponseObject.getString("resMsg"));
                }

                courseService.compareSyncCourseRes(courseUserList, courseResList);


                /*String updateStatUrl = rrtbkUrl + updateStat+orgId+"?accessToken=" + OpenUtil.createTokenByUrl(rrtbkUrl);
                HttpClient.doGetMethod(updateStatUrl);*/
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("sync data task failed:" + e.getMessage());
            }

            logger.info("ending sync data task");
        }
    }
}
